home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16306 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.8 KB

  1. Path: news.compuserve.com!newsmaster
  2. From: 100435.736@compuserve.com (David A. Mair)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: How do you return values from in-line asm?
  5. Date: Wed, 10 Apr 1996 09:36:07 GMT
  6. Organization: CompuServe Incorporated
  7. Message-ID: <4kfvec$s6k@arl-news-svc-4.compuserve.com>
  8. References: <4j9mab$19si@usenetp1.news.prodigy.com>
  9. NNTP-Posting-Host: dd41-171.compuserve.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. XKWR65B@prodigy.com (Mark Rubelmann) wrote:
  13.  
  14.  
  15. Firstly, let me state for the record that this is not a C++ question
  16. and you should perhaps have posted it in an MS-DOS or Intel assembly
  17. language group.  You question is really off the topic that this group
  18. deals with.  However,...
  19.  
  20. >First of all, I just don't understand how the resut went from al to ax. 
  21.  
  22. This is simple, al is part of ax.  The ax register is 16 bits wide and
  23. al is a synonym for the low byte and ah is a synonym for the high
  24. byte.  Therefore ax = ahal (if you see what I mean).
  25.  
  26. >Also, I know the line labels should be outside of the asm statement but 
  27. >that's the way it was in the book.
  28.  
  29. The book was clearly wrong.
  30.  
  31. > I've tried fooling around with ret and 
  32. >stuff like that but I can't get it to work. The compiler always gives a 
  33. >warning that it should return a value.
  34.  
  35. The compiler operates on the source code provided and if you include
  36. an early return the compiler continues to compile until it finds the
  37. close brace matching the open brace after the declaration.  If your
  38. function has a return type the compiler must see a:
  39.  
  40.       return <lvalue of return type>;
  41.  
  42. Here's how I do this (using your code):
  43.  
  44. unsigned char Get_Scan_Code(void)
  45. {
  46. unsigned char byRetVal = 0;
  47.  
  48. asm {
  49.      mov ah,01h        //Function 1: is a key ready?
  50.      int 16h                // Call interrupt
  51.      jz done               // No key, exit *** byRetVal is already 0
  52.      mov ah,00h        // Function 0: get scan code
  53.      int 16h                // Call interrupt
  54. // Not required because we can move ah to the return var
  55. //     mov al,ah           // result was in ah so put it in al
  56. //     xor ah,ah            // zero out ah
  57.  
  58.      mov byte ptr [byRetVal], ah
  59.  
  60. // Replaced by initialising return variable to zero    
  61. //     jmp done            // the data's in ax    (??? ax???)
  62. //     
  63. //empty:
  64. //     xor ax,ax            // clear out ax, 0 means no key
  65.      }
  66. done:
  67.  
  68.      return byRetVal;
  69. }
  70.  
  71. >When I try to use the function in 
  72. >something like:
  73.  
  74. >     while(Get_Scan_Code==0) {}
  75.  
  76. >but it doesn't work, it just goes on to the next statement. Any help 
  77. >would be appreciated.
  78.  
  79. That's a little unusual, but I suspect your compiler uses ax or
  80. restores a value into it because you have not implied its use by
  81. including a return statement.  Or, perhaps, your compiler pretends the
  82. function is void because it doesn't return a value and (void == 0) is
  83. false.
  84.  
  85. Regards
  86. David.
  87.  
  88.  
  89.